package com.centronix.manager.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centronix.bean.ComboBean;
import com.centronix.dao.ExpenseDAO;
import com.centronix.dao.ExpenseTypeDAO;
import com.centronix.domain.Expense;
import com.centronix.domain.ExpenseType;
import com.centronix.manager.ExpenseManager;
@Service(value="expenseManager")
@Transactional
public class ExpenseManagerImpl implements ExpenseManager , Serializable{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ExpenseDAO expenseDAO;
	
	@Autowired
	private ExpenseTypeDAO expenseTypeDAO;

	public List<Expense> findAllExpense(){
		return expenseDAO.findAll();
	}
	
	public Boolean saveExpense(Expense exp){
		expenseDAO.saveAndFlush(exp);
		return true;
	}
	
	public Boolean deleteExpense(int expId){
		expenseDAO.delete(expId);
		return true;
	}
	
	public List<Expense> filterExpense(Date expenseDate){
		return expenseDAO.findByDate(expenseDate);
	}
	
	public List<ComboBean> getExpenseTypeCombo(){
		List<ComboBean> comboBeans = new ArrayList<>();
		comboBeans.add(ComboBean.getDefaultCombo());
		List<ExpenseType> expenseTypes= expenseTypeDAO.findAll();
		ComboBean comboBean = null;
		for (ExpenseType expenseType : expenseTypes) {
			comboBean = new ComboBean();
			copyEmployeeToComboBean(expenseType, comboBean);
			comboBeans.add(comboBean);
		}
		return comboBeans;
	}
	
	public void copyEmployeeToComboBean(final ExpenseType expenseType, final ComboBean comboBean) {
		comboBean.setValue(String.valueOf(expenseType.getExpenseTypeId()));
		comboBean.setLabel(expenseType.getNarrative());
	}
	
	public ExpenseDAO getExpenseDAO() {
		return expenseDAO;
	}

	public void setExpenseDAO(ExpenseDAO expenseDAO) {
		this.expenseDAO = expenseDAO;
	}

	public ExpenseTypeDAO getExpenseTypeDAO() {
		return expenseTypeDAO;
	}

	public void setExpenseTypeDAO(ExpenseTypeDAO expenseTypeDAO) {
		this.expenseTypeDAO = expenseTypeDAO;
	}
	
	
}
